<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">从业人员</text>
				<view class="data-form-input" @click="openSelect">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.nickName"
						:placeholder="optionDisabled?'-':'请输入从业人员'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">用户姓名</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.name"
						:placeholder="optionDisabled?'-':'请输入用户姓名'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">用户性别</text>
				<view class="data-form-input">
					<cpdd-select v-model="dataForm.gender" :disabled="optionDisabled" :list="genderList" keyname="name"
						valname="val"></cpdd-select>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">岗位</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.postName"
						:placeholder="optionDisabled?'-':'请输入岗位'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">健康证号</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.cardNo"
						:placeholder="optionDisabled?'-':'请输入健康证号'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">健康证图片</text>
				<view class="data-form-input">
					<gui-upload-images2 @change="choosePhoto" :header="header" fileName="file" ref="uploadimgcom"
						@uploaded="uploaded" :uploadServerUrl="`${$http.config.baseUrl}/common/upload`"
						:maxFileNumber="1"></gui-upload-images2>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">有效期开始</text>
				<view class="data-form-input">
					<cpdd-date v-model="dataForm.expireTimeStart" :disabled="optionDisabled"
						:isTime="false"></cpdd-date>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30">有效期结束</text>
				<view class="data-form-input">
					<cpdd-date v-model="dataForm.expireTimeEnd" :disabled="optionDisabled" :isTime="false"></cpdd-date>
				</view>
			</view>
		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
		<cpdd-user-select ref="userSelectRef" @change="userChange"></cpdd-user-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				needPploadedImgs: [],
				dataForm: { //表单
					expireTimeEnd: '',
					expireTimeStart: '',
					postName: '',
					gender: '',
					name: '',
					cardImage: '',
					cardNo: '',
					userId: '',
					nickName: ''
				},
				header: {
					"Authorization": uni.getStorageSync('lifeData')?.vuex_token
				},
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				genderList: [{
					name: '男',
					val: '男'
				}, {
					name: '女',
					val: '女'
				}],
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			if (options.op != 'add') {
				this.loadDetail();
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			loadDetail() {
				this.$http.get(`/worker/healthRecord/${this.did}`).then(res => {
					if (res.code == 200) {
						let {
							id,
							name,
							userId,
							postName,
							expireTimeEnd,
							expireTimeStart,
							gender,
							cardImage,
							cardNo
						} = res.data;
						this.dataForm.id = id;
						this.dataForm.name = name;
						this.dataForm.userId = userId;
						this.dataForm.postName = postName;
						this.dataForm.expireTimeEnd = expireTimeEnd;
						this.dataForm.expireTimeStart = expireTimeStart;
						this.dataForm.gender = gender;
						this.dataForm.cardImage = cardImage;
						this.dataForm.cardNo = cardNo;
						this.$refs.uploadimgcom.setItems([cardImage]);
					}
					console.log('', result)
				})
			},
			userChange(e) {
				this.dataForm.userId = e.userId;
				this.dataForm.nickName = e.nickName;
			},
			openSelect() {
				this.$refs.userSelectRef.open();
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				console.log('提交');
				if (this.needPploadedImgs.length < 1) {
					this.$u.msg("请上传健康证")
					return;
				}
				if (this.needPploadedImgs.length > 0) {
					this.$refs.uploadimgcom.upload();
					return;
				} else {
					this.realSumbit();
				}
			},
			realSumbit() {
				if (this.dataForm.id) {
					this.$http.put('/worker/healthRecord', {
						id: this.dataForm.id,
						userId: this.dataForm.userId,
						name: this.dataForm.name,
						gender: this.dataForm.gender,
						cardNo: this.dataForm.cardNo,
						cardImage: this.dataForm.cardImage,
						postName: this.dataForm.postName,
						expireTimeStart: this.dataForm.expireTimeStart,
						expireTimeEnd: this.dataForm.expireTimeEnd,
					}).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/worker/healthRecord', {
						userId: this.dataForm.userId,
						name: this.dataForm.name,
						gender: this.dataForm.gender,
						cardNo: this.dataForm.cardNo,
						cardImage: this.dataForm.cardImage,
						postName: this.dataForm.postName,
						expireTimeStart: this.dataForm.expireTimeStart,
						expireTimeEnd: this.dataForm.expireTimeEnd,
					}).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			},
			choosePhoto(e) { //选择图片
				console.log(e);
				this.needPploadedImgs = e;
			}, // 图片上传完毕执行 uploaded 函数
			uploaded: function(uploadedImgs) {
				console.log('图片上传完毕，开始提交数据');
				// 组合数据给后端 api 提交
				console.log(uploadedImgs);
				let arr = []
				for (var i = 0; i < uploadedImgs.length; i++) {
					arr.push(uploadedImgs[i].url)
				}
				this.dataForm.cardImage = arr.join();
				console.log(this.dataForm);
				this.realSumbit();
			},

		}
	}
</script>

<style lang="scss">
	.mr20 {
		margin-right: 20rpx;
	}

	.radio_txt {
		margin-left: 8rpx;
		position: relative;
		top: 5rpx;
		font-size: 28rpx;
	}
</style>